<template>
  <div class="flex justify-center pt-4" v-if="!BdListComponent">
     <el-card class="box-card w-1/2 align dark:bg-black" >
        <el-form-item label="产品ID" class="ml-7  ">
          <el-input v-model="appcd" readonly class="p_sn bg-gray-600"></el-input>
        </el-form-item>
        <el-form-item label="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;卡&nbsp;&nbsp;密">
          <el-input v-model="appcdkey" type="textarea"  :rows="6"  ></el-input>
        </el-form-item>
        <div class="flex justify-end">
           <el-form-item >
              <el-button type="info"  v-if="appcdkey.length==0"  disabled  >...</el-button>
              <el-button  type="primary"  v-else  @click="enterHandle()">确定</el-button>
           </el-form-item>
         </div>
    </el-card>
  </div>
   
</template>


<script setup>
import { ref, onMounted,getCurrentInstance,watch } from 'vue';
import { ElMessage ,ElMessageBox ,ElDrawer } from 'element-plus'
import { useBdStore } from '@/stores/bdStore.js' 
import { useRouter } from 'vue-router'
const router = useRouter();

const useStore = useBdStore()
const appcd = ref('')
const appcdkey = ref('')

let first_start = {}
const BdListComponent = ref(false);

onMounted(()=>{
    setTimeout(()=>{
       pyinit();
    },300)
})

const pyinit = async () => {
    appcd.value = await get_app_serial();
}

const get_app_serial = async() => {
  return new Promise((resolve, reject) => {
    window.pywebview.api.py_get_app_serial().then((r) => {
         resolve(r)
    })
  })
}

function isDateBeforeNow(dateString,day=30) {
  return new Promise((resolve, reject)=>{
      const inputDate = new Date(dateString.replace(/-/g, '/'));
      const addedDate = new Date(inputDate.setDate(inputDate.getDate() + day));
      resolve(addedDate)
  })
}



// // 使用示例
// const yearMonthDay = "2023-11-08"; // 请将这个字符串替换为您想要检查的日期
// const result = isDateBeforeNow(yearMonthDay);
// console.log(result); // 如果日期小于当前时间，将输出true

const enterHandle = async() => {
  if (!appcdkey.value) {
    return false
  }
  if (await __appcdkeyHandle(appcdkey.value) == false) {
     return ElMessage.error("无效的卡密")
  } else {
     return  router.push({ path: '/bdList' });
  }
}

const __appcdkeyHandle = async (appcdkeyStr) => {
     return new Promise((resolve, reject)=>{
          if (appcdkeyStr=='') {
               useStore.app_usage_time_status = 2      //需要注册
               useStore.setAppUsageTimeTxt("无效的卡密",2)
               resolve(false)
          }
          window.pywebview.api.py_unAppcodekey(appcdkeyStr).then((r) => {
             if (r.status==true) {
                 useStore.app_usage_time_status = 1
                 useStore.setAppUsageTimeTxt('有效时间:'+r.msg,1)
                 console.log('有效时间:'+r.msg)
                 window.pywebview.api.pymaterial_updateTbVariables({'key':'appcdkey','value':appcdkey.value,'category':'app'}).then(r=>{
                      resolve(true)
                 });
             } else {
                 useStore.app_usage_time_status = 2      //需要注册
                 useStore.setAppUsageTimeTxt(r.msg,2)
                 window.pywebview.api.pymaterial_updateTbVariables({'key':'appcdkey','value':'','category':'app'});
                 resolve(false)
             }
         })
     })

}

</script>